import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/index.js'
import Layout from '@/views/layout/LayoutContainer.vue'
import layout from '@/views/layout/LayoutContainer.vue'
import sysRole from '@/views/system/sysRole.vue'
import sysUser from '@/views/system/sysUser.vue'
import sysMenu from '@/views/system/sysMenu.vue'
import login from '@/views/login/LoginPage.vue'
import generateImage from '@/views/dataGeneration/generateImage.vue'
import project from '@/views/testFlow/project.vue'
import version from '@/views/testFlow/version.vue'
import testRequest from '@/views/testFlow/testRequest.vue'
import myTodo from '@/views/testFlow/myTodo.vue'
//创建路由实例
let router
router = createRouter({
  // 配置history模版
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: login,
      name: 'login',
      meta: {
        title: '登录'
      },
      icon: 'Location'
    },
    {
      path: '/',
      component: layout,
      name: 'layout',
      meta: {
        title: 'layout'
      },
      icon: 'Location'
    },
    {
      path: '/system',
      component: Layout,
      name: 'system',
      meta: {
        title: '系统管理'
      },
      icon: 'Location',
      children: [
        {
          path: 'sysRole',
          name: 'sysRole',
          component: sysRole,
          meta: {
            title: '角色管理'
          },
          hidden: false
        },
        {
          path: 'sysUser',
          name: 'sysUser',
          component: sysUser,
          meta: {
            title: '用户管理'
          },
          hidden: false
        },
        {
          path: 'menu',
          name: 'sysMenu',
          component: sysMenu,
          meta: {
            title: '菜单管理'
          },
          hidden: false
        }
      ]
    },
    {
      path: '/111',
      component: Layout,
      name: 'system3',
      meta: {
        title: '造数中心'
      },
      icon: 'Location',
      children: [
        {
          path: '/generateImage',
          name: 'generateImage',
          component: generateImage,
          meta: {
            title: '生成测试图片'
          },
          hidden: false
        }
      ]
    },
    {
      path: '/testFlow',
      component: Layout,
      name: 'testFlow',
      meta: {
        title: '提测中心'
      },
      icon: 'Location',
      children: [
        {
          path: 'project',
          name: 'project',
          component: project,
          meta: {
            title: '项目管理'
          },
          hidden: false
        },
        {
          path: 'version',
          name: 'version',
          component: version,
          meta: {
            title: '版本管理'
          },
          hidden: false
        },
        {
          path: 'testRequest',
          name: 'testRequest',
          component: testRequest,
          meta: {
            title: '提测单管理'
          },
          hidden: false
        },
        {
          path: 'myTodo',
          name: 'myTodo',
          component: myTodo,
          meta: {
            title: '我的待办'
          },
          hidden: false
        }
      ]
    }
  ]
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') {
    return '/login'
  }
})

export default router
